package com.lxl.securitypermission.controller;

import cn.hutool.core.lang.Assert;
import com.lxl.securitypermission.constant.HttpCode;
import com.lxl.securitypermission.controller.vo.MenuVo;
import com.lxl.securitypermission.dto.ResponseResult;
import com.lxl.securitypermission.service.MenuService;
import com.lxl.securitypermission.vo.TreeMenu;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author 绫小路
 * @date 2020/12/30 22:35
 * @description
 */
@Api(value = "MenuController", tags = "MenuController")
@Controller
public class MenuController {

  @Autowired
  private MenuService menuService;

  /**
   * 授权后才能访问
   */
  @ApiOperation("获取登录者的菜单，登录后才能访问")
  @PreAuthorize("isAuthenticated()")
  @GetMapping("menu/get")
  @ResponseBody
  public ResponseResult get(HttpServletRequest request) {
    //CustomAuthenticationSuccessHandler.java 处已经把菜单存到会话中
    return new ResponseResult(HttpCode.OK, request.getSession().getAttribute("menu"));
  }

  @ApiOperation("菜单管理页面")
  @GetMapping("/admin/menu")
  public String index() {
    return "admin/admin-menu";
  }

  @ApiOperation("获取所有菜单的tree数据")
  @GetMapping("/admin/menu/tree")
  @ResponseBody
  public ResponseResult<List<TreeMenu>> tree(Integer status) {
    return menuService.getTreeMenu(status);
  }

  @ApiOperation("获取菜单的详细信息，根据菜单id")
  @GetMapping("admin/menu/info")
  @ResponseBody
  public ResponseResult info(Integer id) {
    if (id == null) {
      return null;
    }
    return menuService.info(id);
  }

  @ApiOperation("菜单选择图片页面")
  @GetMapping("admin/menu/selectIcon")
  public String selectIcon() {
    return "admin/select-icon";
  }

  @ApiOperation("更新菜单")
  @PostMapping("admin/menu/update")
  @ResponseBody
  public ResponseResult update(@RequestBody MenuVo menuVo) {
    Assert.isFalse(menuVo.getId() == null, "入参Id不能为空！");
    Assert.isFalse(menuVo.getStatus() == null, "菜单状态不能为空！");
    Assert.isFalse(menuVo.getSortId() == null, "排序Id不能为空！");
    Assert.isFalse(menuVo.getSortId() > 999 || menuVo.getSortId() < 1, "排序Id不能大于999或者小于1");
    Assert.notBlank(menuVo.getHref(), "菜单URL不能为空！");
    Assert.notBlank(menuVo.getName(), "菜单名字不能为空！");
    Assert.notBlank(menuVo.getIcon(), "菜单图标不能为空！");
    return menuService.update(menuVo);
  }

  @ApiOperation("添加菜单页面")
  @GetMapping("admin/menu/add")
  public String add() {
    return "admin/admin-menu-add";
  }

  @ApiOperation("添加菜单")
  @PostMapping("admin/menu/add")
  @ResponseBody
  public ResponseResult addPost(@RequestBody MenuVo menuVo) {
    Assert.isFalse(menuVo.getPid() == null, "请选择一个父菜单再添加！");
    Assert.isFalse(menuVo.getStatus() == null, "菜单状态不能为空！");
    Assert.isFalse(menuVo.getSortId() == null, "排序Id不能为空！");
    Assert.isFalse(menuVo.getSortId() > 999 || menuVo.getSortId() < 1, "排序Id不能大于999或者小于1");
    Assert.notBlank(menuVo.getHref(), "菜单URL不能为空！");
    Assert.notBlank(menuVo.getName(), "菜单名字不能为空！");
    Assert.notBlank(menuVo.getIcon(), "菜单图标不能为空！");
    return menuService.add(menuVo);
  }

  @ApiOperation("删除某个菜单")
  @PostMapping("admin/menu/delete")
  @ResponseBody
  public ResponseResult delete(Integer id) {
    if (id == null) {
      throw new IllegalArgumentException("请选择一个菜单再删除");
    }
    return menuService.delete(id);
  }

  @GetMapping("admin/menu/permission")
  public String permission() {
    return "admin/admin-menu-permission";
  }

  @GetMapping("admin/menu/permissionInfo")
  @ResponseBody
  public ResponseResult permissionInfo(Integer menuId) {
    return menuService.permissionInfo(menuId);
  }

  @PostMapping("admin/menu/updatePermission")
  @ResponseBody
  public ResponseResult updatePermission(Integer menuId, String roleIds) {
    Assert.isFalse(menuId == null, "入参menuId不能为空！");
    return menuService.updatePermission(menuId, roleIds);
  }
}
